package org.szns.android.amarket;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class aMarket extends Activity {
    /** Called when the activity is first created. */
	public static final String DB_NAME = "amarket.sqlite";
    public static final int VERSION = 1;

    SQLiteDatabase db;
	DatabaseHelper dh;

	TextView appall;
	EditText searchKey;

    @Override
    // 入口
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        index();
    }

    // 首页
    public void index()
    {
    	setContentView(R.layout.main);
    	appall = (TextView) findViewById(R.id.appall);
    	searchKey = (EditText) findViewById(R.id.searchKey);

    	appall.setOnClickListener(new View.OnClickListener() {
    		public void onClick(View v) {
    			list();
    		}
    	});

    }

    public void list()
    {
    	setContentView(R.layout.list);
    	ListView list = (ListView) findViewById(R.id.AppListView);
        ArrayList <HashMap <String, String>> mylist = new ArrayList <HashMap <String, String>>();


        dh = new DatabaseHelper(this, DB_NAME, null, VERSION);
        db = dh.getWritableDatabase();

        Cursor result = db.query("apps", null, null, null, null, null, null);
        result.moveToFirst();
        while (!result.isAfterLast()) {
            HashMap <String, String> map = new HashMap <String, String>();
            map.put("name", result.getString(1));
            map.put("size", result.getString(10));
        	map.put("download", result.getString(14));
        	mylist.add(map);
            result.moveToNext();
          }
        result.close();

        SimpleAdapter mSchedule = new SimpleAdapter(this, //没什么解释
        		mylist, //数据来源
                R.layout.app_listitem, //ListItem的XML实现
                new String[] {"name", "size", "download"}, //动态数组与ListItem对应的子项
                new int[] {R.id.name, R.id.size, R.id.download}); //ListItem的XML文件里面的两个TextView ID
        		list.setAdapter(mSchedule); //添加并且显示
    }

    public void view()
    {

    }

    public void search()
    {

    }

    public void download()
    {

    }
}